const query = require('../../models');

module.exports = {
  total(req, res) {
    let sql, params;
    // type: all-所有 hot-热门 cat=分类 usr-用户
    const { type } = req.query;

    switch (type) {
      case 'all':
        sql = `select count(*) as total from activities where is_del=1`;
        params = [];
        break;
      case 'cat':
        sql = `select count(*) as total from activities where activity_category_id=? and is_del=1`;
        params = [req.query.categoryId];
        break;
      case 'usr':
        sql = `select count(*) as total from activities where activity_uid=? and is_del=1`;
        params = [req.query.userId];
        break;
    }

    query(sql, params)
      .then(result => {
        console.log('查询数量的结果：', result);
        res.json({
          code: '200',
          errMsg: '操作成功',
          data: result[0].total
        })
      })
      .catch(err => {
        res.json({
          code: '1001',
          errorMsg: '操作失败',
          err: err
        })
      });
  },
  list(req, res) {
    let sql, params;
    // type: all-所有 hot-热门 cat=分类 usr-用户
    let { type, currentPage, pageSize } = req.query;
    currentPage = Number(currentPage);
    pageSize = Number(pageSize);
    let start = (currentPage - 1) * pageSize;

    switch (type) {
      case 'all':
        sql = `SELECT activity_id,activity_name,activity_category_id,activity_limit_num,activity_current_num,created_at,state,activity_uid FROM activities WHERE is_del=1 ORDER BY created_at DESC LIMIT 0,10 ?,?`;
        params = [start, pageSize];
        break;
      case 'hot':
        sql = `SELECT activity_id,activity_name,activity_category_id,activity_limit_num,activity_current_num,created_at,state,activity_uid FROM activities order by hot desc limit 0,3`;
        params = [];
        break;
      case 'cat':
        sql = `SELECT activity_id,activity_name,activity_category_id,activity_limit_num,activity_current_num,created_at,state,activity_uid FROM activities WHERE activity_category_id=? order by created_at desc limit ?,?`;
        params = [req.query.categoryId, start, pageSize];
        break;
      case 'usr':
        sql = `SELECT activity_id,activity_name,activity_category_id,activity_limit_num,activity_current_num,created_at,state,activity_uid FROM activities WHERE activity_uid=? order by created_at desc limit ?,?`;
        params = [req.query.userId, start, pageSize];
        break;
    }

    query(sql, params)
      .then(result => {
        console.log('查询活动列表结果：', result);
        res.json({
          code: '200',
          errMsg: '操作成功',
          data: result
        })
      })
      .catch(err => {
        res.json({
          code: '1001',
          errorMsg: '操作失败',
          err: err
        })
      });
  },
  userList(req, res) {
    let { currentPage, pageSize, userId } = req.query;
    currentPage = Number(currentPage);
    pageSize = Number(pageSize);
    let start = (currentPage - 1) * pageSize;

    let sql = `select * from activities where activity_uid=? order by created_at desc limit ?,?`;
    let params = [userId, start, pageSize];

    query(sql, params)
      .then(result => {
        console.log('查询活动列表结果：', result);
        res.json({
          code: '200',
          errMsg: '操作成功',
          data: result
        })
      })
      .catch(err => {
        res.json({
          code: '1001',
          errorMsg: '操作失败',
          err: err
        })
      });
  },
  category(req, res) {
    const sql = `select * from activity_category`;

    query(sql, [])
      .then(result => {
        res.json({
          code: '200',
          errMsg: '操作成功',
          data: result
        })
      })
      .catch(err => {
        res.json({
          code: '1001',
          errorMsg: '操作失败',
          err: err
        })
      });
  },
  getById(req, res) {
    const sql = `select * from activities where activity_id=?`;
    const activityId = req.query.activityId

    query(sql, [activityId])
      .then(result => {
        res.json({
          code: '200',
          errMsg: '操作成功',
          data: result[0]
        })
      })
      .catch(err => {
        res.json({
          code: '1001',
          errorMsg: '操作失败',
          err: err
        })
      });
  },
  public(req, res) {
    const sql = `INSERT INTO activities VALUES(NULL,?,?,?,?,?,?,DEFAULT,?,?,DEFAULT,0,?,DEFAULT)`;
    const { activityName, activityContent, activityCategoryId, activityAddress, activityLimitNum, activityCurrentNum, activityStart, activityEnd, activityUid
    } = req.body;

    query(sql, [activityName, activityContent, activityCategoryId, activityAddress, activityLimitNum, activityCurrentNum, activityStart, activityEnd, activityUid])
      .then(result => {
        res.json({
          code: '200',
          errMsg: '操作成功'
        })
      })
      .catch(err => {
        res.json({
          code: '1001',
          errorMsg: '操作失败',
          err: err
        })
      });
  },
  editOne(req, res) {
    const sql = ``;

    query(sql, [])
      .then(result => {
        res.json({
          code: '200',
          errMsg: '操作成功',
          data: {}
        })
      })
      .catch(err => {
        res.json({
          code: '1001',
          errorMsg: '操作失败',
          err: err
        })
      });
  },
  delById(req, res) {
    const sql = ``;

    query(sql, [])
      .then(result => {
        res.json({
          code: '200',
          errMsg: '操作成功',
          data: {}
        })
      })
      .catch(err => {
        res.json({
          code: '1001',
          errorMsg: '操作失败',
          err: err
        })
      });
  }
}